package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import adapter.BooksRecAdaper;
import base.BaseFragment;
import bean.BooksBean;
import presenter.BooksPresenter;
import utils.ToastUtil;
import view.BooksView;

/**
 * Created by 裘翔 on 2019/11/6.
 */

public class BooksFragment extends BaseFragment<BooksView, BooksPresenter> implements BooksView {
    private View view;
    private RecyclerView mBookRec;
    private SmartRefreshLayout mBookSmc;
    private ArrayList<BooksBean> list;
    private BooksRecAdaper recAdaper;


    @Override
    protected BooksPresenter bindPresenter() {
        return new BooksPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.loadData();
    }

    @Override
    protected void initListener() {

    }

    public void initView(View view) {
        mBookRec = (RecyclerView) view.findViewById(R.id.book_rec);
        mBookSmc = (SmartRefreshLayout) view.findViewById(R.id.book_smc);
        mBookRec.setLayoutManager(new LinearLayoutManager(getContext()));
        list = new ArrayList<>();
        recAdaper = new BooksRecAdaper(getActivity(), list);
        mBookRec.setAdapter(recAdaper);
        recAdaper.setOnClickItem(new BooksRecAdaper.OnClickItem() {
            @Override
            public void setonClickItemListener(View v, int position) {
                Intent intent = new Intent(getContext(), BooksDatilActivity.class);
                intent.putExtra("id",list.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.books_fragment;
    }

    @Override
    public void showToast(String msg) {

    }


    @Override
    public void onSuccess(List<BooksBean> booksBean) {
           list.addAll(booksBean);
           recAdaper.notifyDataSetChanged();
    }

    @Override
    public void onFailed(String str) {
        ToastUtil.show(getContext(),str);
    }
}
